package myreactor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class AcceptorHandler extends AbstractHandler{

	public AcceptorHandler(Selector seletor,int port) {
		try {
			ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
			serverSocketChannel.bind(new InetSocketAddress(port));
			serverSocketChannel.configureBlocking(false);
			serverSocketChannel.register(seletor, SelectionKey.OP_ACCEPT,this);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void handle(SelectionKey selectionKey) {
		
		try {
			ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
			SocketChannel socketChannel = serverSocketChannel.accept();
			socketChannel.configureBlocking(false);
			MyReactor subReactor = ReactorManger.disPatchReactor();
			Selector selector = subReactor.getSelector();
			socketChannel.register(selector, SelectionKey.OP_READ,new BusinessHandler());
			selector.wakeup();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
